﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Controls_SelectAdvisorSurveyor : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserId"] != null)
        {
            Business.ReturnUserDetail d = new Business.ReturnUserDetail();
            Lblstatus.Text =" مشاور / نقشه بردار فعلی : "+ d.ReturnName(new Guid(Session["UserId"].ToString()));
            Lblstatus.CssClass = "success";
        }
    }

    protected void RadioButtonListRole_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (RadioButtonListRole.SelectedItem.Value == "Advisor")
        {
            RadioButtonListAdvisors.Visible = true;
            RadioButtonListSurveyor.Visible = false;
        }
        else
        {
            RadioButtonListSurveyor.Visible = true;
            RadioButtonListAdvisors.Visible = false;
        }
    }
    protected void RadioButtonListAdvisors_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void GridViewUsers_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            int id = Convert.ToInt32(e.CommandArgument);
            switch (e.CommandName)
            {
                case "Select":
                    Guid userid = new Guid(GridViewUsers.DataKeys[id].Value.ToString());
                    string NameFamily = GridViewUsers.Rows[id].Cells[0].Text + " , " + GridViewUsers.Rows[id].Cells[1].Text;

                    Session["UserIdNew"] = userid;
                    Session["NameFamily"] = NameFamily;
   Lblstatus.Text =" مشاور / نقشه بردار جدید : "+ NameFamily;
            Lblstatus.CssClass = "success";
                    break;
            }
        }
        catch
        { }
    }
    public void cleargrid()
    {
        GridViewUsers.DataSource = "";
        GridViewUsers.DataBind();
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        try
        {
            if (RadioButtonListRole.SelectedItem.Value == "Advisor")
            {
                if (RadioButtonListAdvisors.SelectedItem.Value == "Advisorhoghooghi")
                {
                    SubSonic.Query q = DataAccess.TblUserDetail.CreateQuery();
                    q.QueryType = SubSonic.QueryType.Select;
                    q.AddWhere(DataAccess.TblUserDetail.Columns.Family, SubSonic.Comparison.Like, "%" + TxtFamily.Text + "%");
                    q.AddWhere(DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Advisorhoghooghi");
                    DataSet dt = q.ExecuteDataSet();
                    if (dt.Tables[0].Rows.Count > 0)
                    {
                        Lblstatus.Text = " ";
                        Lblstatus.CssClass = "";
                        GridViewUsers.DataSource = dt;
                        GridViewUsers.DataBind();
                    }
                    else
                    {
                        Lblstatus.Text = " .فرد مورد نظر یافت نشد ";
                        Lblstatus.CssClass = "error";
                        cleargrid();
                    }
                }
                if (RadioButtonListAdvisors.SelectedItem.Value == "Advisorhaghighi")
                {
                    SubSonic.Query q = DataAccess.TblUserDetail.CreateQuery();
                    q.QueryType = SubSonic.QueryType.Select;
                    q.AddWhere(DataAccess.TblUserDetail.Columns.Family, SubSonic.Comparison.Like, "%" + TxtFamily.Text + "%");
                    q.AddWhere(DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Advisorhaghighi");
                    DataSet dt = q.ExecuteDataSet();
                    if (dt.Tables[0].Rows.Count > 0)
                    {

                        Lblstatus.Text = " ";
                        Lblstatus.CssClass = "";
                        GridViewUsers.DataSource = dt;
                        GridViewUsers.DataBind();
                    }
                    else
                    {
                        Lblstatus.Text = " .فرد مورد نظر یافت نشد ";
                        Lblstatus.CssClass = "error";
                        cleargrid();
                    }
                }
            }
            if (RadioButtonListRole.SelectedItem.Value == "Surveyor")
            {
                if (RadioButtonListSurveyor.SelectedItem.Value == "hoghooghi")
                {
                    SubSonic.Query q = DataAccess.TblUserDetail.CreateQuery();
                    q.QueryType = SubSonic.QueryType.Select;
                    q.AddWhere(DataAccess.TblUserDetail.Columns.Family, SubSonic.Comparison.Like, "%" + TxtFamily.Text + "%");
                    q.AddWhere(DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Surveyorhoghooghi");
                    DataSet dt = q.ExecuteDataSet();
                    if (dt.Tables[0].Rows.Count > 0)
                    {

                        Lblstatus.Text = " ";
                        Lblstatus.CssClass = "";
                        GridViewUsers.DataSource = dt;
                        GridViewUsers.DataBind();
                    }
                    else
                    {
                        Lblstatus.Text = " .فرد مورد نظر یافت نشد ";
                        Lblstatus.CssClass = "error";
                        cleargrid();
                    }
                }
                if (RadioButtonListSurveyor.SelectedItem.Value == "haghighi")
                {
                    SubSonic.Query q = DataAccess.TblUserDetail.CreateQuery();
                    q.QueryType = SubSonic.QueryType.Select;
                    q.AddWhere(DataAccess.TblUserDetail.Columns.Family, SubSonic.Comparison.Like, "%" + TxtFamily.Text + "%");
                    q.AddWhere(DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Surveyorhaghighi");
                    DataSet dt = q.ExecuteDataSet();
                    if (dt.Tables[0].Rows.Count > 0)
                    {

                        Lblstatus.Text = " ";
                        Lblstatus.CssClass = "";
                        GridViewUsers.DataSource = dt;
                        GridViewUsers.DataBind();
                    }
                    else
                    {
                        Lblstatus.Text = " .فرد مورد نظر یافت نشد ";
                        Lblstatus.CssClass = "error";
                        cleargrid();
                    }
                }
            }
        }
        catch
        { }
    }
    protected void GridViewUsers_RowDataBound(object sender, GridViewRowEventArgs e)
    {


    }
}